package com.mit.web.business;

import org.apache.log4j.Logger;

import com.mit.common.RequestContext;
import com.mit.common.ResultContext;
import com.mit.common.exception.BaseException;
import com.mit.facade.BusinessFacade;

/**
 * When we use remote interface, ServiceLocator can reduce the 
 * 
 * @author River Yu
 *
 */
public class ServiceLocator {
	
	private static ServiceLocator me;
	private Logger logger = Logger.getLogger(ServiceLocator.class);
	private BusinessFacade bizFacade;
	private ServiceLocator(){
		bizFacade = new BusinessFacade();
	}
	
	public static ServiceLocator getInstance(){
		if(me == null)
			me = new ServiceLocator();
		return me;
	}
	
	public ResultContext doBusiness(RequestContext rqs) throws BaseException {
		//TODO save UserId in the requestContext
		//TODO Logging???
		//TODO how to handle info, warning, and error message?!
		logger.debug("[call remote service] -- begin");
		long timebegin = System.currentTimeMillis();
		ResultContext rlt = new ResultContext();
		try{
			rlt = bizFacade.doBusiness(rqs);
		}catch(BaseException e){
			throw e;
		}catch(Exception e){
			BaseException be = new BaseException(e.getMessage(), e);
			throw be;
		}
		long timeend = System.currentTimeMillis();
		logger.debug("[call remote service] -- Finish (" + (timeend - timebegin) +  " MS)");
		return rlt;
	}

}
